import collections

Card = collections.namedtuple('Card', ['rank', 'suit'])
# namedtuple是一种数据结构，collections.namedtuple('X', ['y', 'z'])打印出来是X(y = ..., z = ...)


class FrenchDeck:
    ranks = [str(n) for n in range(2, 11)] + list('JQKA')
    suits = 'spades diamonds clubs hearts'.split()
    __static_private = 'CONST'
    # 这三条是静态变量，由不同实例共享，除非重新赋值

    def __init__(self):
        self._cards = [Card(rank, suit) for suit in self.suits
                       for rank in self.ranks]
        # 只能允许其本身与子类进行访问。其实也可以直接访问，但IDE会下划线
        self.__private = 1
        print(f"{self.__private} and {FrenchDeck.__static_private} are accessed!")
        # 只能允许类内进行访问

    def __len__(self):
        return len(self._cards)

    def __getitem__(self, position):
        return self._cards[position]


suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)


def spades_high(card):
    rank_value = FrenchDeck.ranks.index(card.rank)
    # card.rank是传入元素的rank
    # FrenchDeck.ranks.index反向查找其在ranks中的位置
    return rank_value * len(suit_values) + suit_values[card.suit]


beer_card = Card('7', 'diamonds')
print(beer_card)
# Card(rank='7', suit='diamonds')

if __name__ == '__main__':
    deck1 = FrenchDeck()
    deck2 = FrenchDeck()
    print(id(deck1.ranks), id(deck2.ranks))
    print(id(deck1._cards), id(deck2._cards))  # 只能允许其本身与子类进行访问。其实也可以直接访问，但IDE会下划线
    # print(deck1.__private)  # 只能允许类内访问
    # print(FrenchDeck.__static_private)  # 只能允许类内访问
    deck1.ranks = [1]
    print(id(deck1.ranks), id(deck2.ranks))
